package com.xhj.bigfactory.bigFactory.blockingQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

import static java.lang.System.out;
import static java.lang.Thread.currentThread;

/**
 * -
 *
 * @author 周沫 @Date 2021-07-30 @Time 1:42
 */
public class BlockingQueueThreadDemo {

  public static void main(String[] args) {
    ArrayBlockingQueue<String> blckingQueue = new ArrayBlockingQueue<>(3);
    new Thread(
            () -> {
              out.println(currentThread().getName() + ": come in...");
              try {
                out.println(blckingQueue.offer("a", 500, TimeUnit.MILLISECONDS));
                out.println(blckingQueue.offer("a", 500, TimeUnit.MILLISECONDS));
                out.println(blckingQueue.offer("a", 500, TimeUnit.MILLISECONDS));
                out.println(blckingQueue.offer("a", 500, TimeUnit.MILLISECONDS));
                blckingQueue.put("a");
                out.println("等到了");
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
            },
            "AAA")
        .start();
    new Thread(
            () -> {
              out.println(currentThread().getName() + ": come in...");
              //			try {String take = blckingQueue.take();out.println("take = " + take);
              //			} catch (Exception e) {e.printStackTrace();}finally{}
              //			try {String take = blckingQueue.take();out.println("take = " + take);
              //			} catch (Exception e) {e.printStackTrace();}finally{}
              //			try {String take = blckingQueue.take();out.println("take = " + take);
              //			} catch (Exception e) {e.printStackTrace();}finally{}
              // 同样的500ms，看谁快
              try {
                Thread.sleep(500);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
              try {
                String take = blckingQueue.take();
                out.println("take = " + take);
              } catch (Exception e) {
                e.printStackTrace();
              } finally {
              }
            },
            "BBB")
        .start();
  }
}
